#include "database.h"
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <cstring>

using namespace std;

/*****************************************************************************/
DataBase::DataBase(){
int i = 1;
if(i == 0 ){
	/*
	int samolot_typ_id;(autogen);	int zasieg_max;	int bak_max; 	
	int pasazerowie_max;int dlugosc_pasa; 	int czas_startu;
	int czas_ladowania;	int predkosc_max; char nazwa[50]; 
	*/
	AddPlane(100,1000,15,1000,14,12,700,"Samolout");
	AddPlane(600,100,15,1010,12,12,500,"Niskolot");
	AddPlane(500,1200,15,1000,14,42,50,"Nielot");
	
	AddPlane(14000,     216840,  543,       2200,    45,   25,    910, "Boeing747");
	AddPlane(12300,     200050,   268,       2100,    36,   23,    876, "Boeing767");
	AddPlane(5420,       85150,   184,       1800,    37,   24,    748, "Boeing737-800");
	AddPlane(11030,      171170,  358,       1800,    47,   28,    900, "Boeing777");
	AddPlane(12500,      210340,  293,       1900,    38,   23,    859, "AirbusA330" );
	AddPlane(15200,      310000,  555,       3000,    48,   28,    1089, "AirbusA380");
	AddPlane(7000,        84040,  180,       1600,    35,   24,    900, "Tu-154");
	AddPlane(4000,       56090,   60,       1200,    35,   23,    545, "An-24");
	AddPlane(3900,        402,     2,        2000,    21,   15,    748, "Cu-27");
	AddPlane(4000,       390,      2,        1000,    20,   15,    740, "Mig-24");

/* void DataBase::AddAirPort(int typ, char nazwa[50], int x, int y, int zasieg,^M
                                          int pojemnosc, int liczbaPasow, int stan){^M
*/
	AddAirPort(1, "Lotnisko1", 0, 100, 100, 100, 20, 8);
	AddAirPort(1, "Lotnisko2", 50, 150, 70, 200, 50, 8);
}
else{
LoadPlanes();
// LoadPorts();
}
}
/*****************************************************************************/
DataBase::~DataBase(){
	airPlaneList.clear();
	airPortList.clear();
}
/*****************************************************************************/
void DataBase::AddPlane(samolot_typ_t newPlane){
	newPlane.samolot_typ_id = airPlaneList.size() + 1 ;
	airPlaneList.push_back(newPlane);
/*	SavePlanes(); */
}
/*****************************************************************************/
void DataBase::AddPlane(int zasieg_max, int bak_max, int pasazerowie_max,
	int dlugosc_pasa, int czas_startu, int czas_ladowania, int predkosc_max,
	char nazwa[50]){
	samolot_typ_t newPlane;
	newPlane.samolot_typ_id = airPlaneList.size() + 1 ;
	newPlane.zasieg_max = zasieg_max;
	newPlane.bak_max = bak_max;
	newPlane.pasazerowie_max = pasazerowie_max;
	newPlane.dlugosc_pasa = dlugosc_pasa;
	newPlane.czas_startu = czas_startu;
	newPlane.czas_ladowania = czas_ladowania;
	newPlane.predkosc_max = predkosc_max
;
	strncpy(newPlane.nazwa, nazwa, 50);
	airPlaneList.push_back(newPlane);				
	cout<<"Dodalem samolot_typ "<<nazwa <<endl;

/*	SavePlanes(); */
}
/*****************************************************************************/
samolot_typ_t DataBase::GetRandomPlane(void){
	int index =  (int)(rand()) % airPlaneList.size();
	if(index >= 0 && index < airPlaneList.size())
		return airPlaneList[index];
	return airPlaneList[0];
	
}
/*****************************************************************************/
void DataBase::AddAirPort(lotnisko_t newAirPort){
//	newAirPort.lotnisko_id = airPortList.size() + 1 ;
	cout<<"Dodalem lotnisko "<<newAirPort.airportName<<endl;
	airPortList.push_back(newAirPort);
/*	SavePorts(); */
}
/*****************************************************************************/
void DataBase::AddAirPort(int typ, char nazwa[50], int x, int y, int zasieg,
					  int pojemnosc, int liczbaPasow, int stan){
	
	
	lotnisko_t newAirPort;
	//newAirPort.lotnisko_id = airPortList.size() + 1 ;
	newAirPort.type = typ;
	strncpy(newAirPort.airportName,nazwa,50);
	newAirPort.X = x;
	newAirPort.Y = y;
	newAirPort.Range = zasieg;
	newAirPort.Capacity = pojemnosc;
	newAirPort.RunwaysNumber = liczbaPasow;
	newAirPort.State = stan;
	airPortList.push_back(newAirPort);
	cout<<"Dodalem lotnisko "<<nazwa<< "x: "<<x<<"y: "<<y<<endl;
/*	SavePorts(); */
}
/*****************************************************************************/
vector<lotnisko_t> DataBase::GetAirPortsList(void){
	/*lotnisko_t *lotniska = new lotnisko_t[10];
	for(int i =0;i<10;++i)
		&lotniska[i] = null;
	for(int i=0, vector<samolot_t>::iterator it = airPortList.begin();i<10,it!=airPortList.end();++i,++it){
		lotniska[i] = *it;
	} */
//	return lotniska;
return airPortList;
}
/*****************************************************************************/

void DataBase::SavePlanes(void){
	ofstream fileAirPlane("airplanes.txt", ios::trunc);
	for(vector<samolot_typ_t>::iterator it = 
airPlaneList.begin(); it != airPlaneList.end(); ++it){
		fileAirPlane << it->samolot_typ_id<<endl;
		fileAirPlane << it->zasieg_max<<endl;
		fileAirPlane << it->bak_max<<endl;
		fileAirPlane << it->pasazerowie_max<<endl;
		fileAirPlane << it->dlugosc_pasa<<endl;
		fileAirPlane << it->czas_startu<<endl;
		fileAirPlane << it->czas_ladowania<<endl;
		fileAirPlane << it->predkosc_max<<endl;
		fileAirPlane << it->nazwa<<endl;
	}	
	fileAirPlane.close();
}
/*****************************************************************************/

void DataBase::LoadPlanes(void){
	ifstream fileAirPlane("airplanes.txt");
	samolot_typ_t newPlane;
	while(!fileAirPlane.eof()){
		fileAirPlane>>newPlane.samolot_typ_id ;
		fileAirPlane>>newPlane.zasieg_max;
		fileAirPlane>>newPlane.bak_max;
		fileAirPlane>>newPlane.pasazerowie_max;
		fileAirPlane>>newPlane.dlugosc_pasa;
		fileAirPlane>>newPlane.czas_startu;
		fileAirPlane>>newPlane.czas_ladowania;
		fileAirPlane>>newPlane.predkosc_max;
		fileAirPlane>>newPlane.nazwa;
		airPlaneList.push_back(newPlane);
		cout<<"Dodalem typ samolotu: "<<newPlane.nazwa<<endl;				
	}
	fileAirPlane.close();
}


/*****************************************************************************/
/*
void DataBase::SavePorts(void){
	ofstream fileAirPort("airports.txt", ios::trunc);
	for(vector<lotnisko_t>::iterator it = 
airPortList.begin(); it != airPortList.end(); ++it){
		fileAirPort << it->lotnisko_id <<endl;
		fileAirPort << it->stan <<endl;
		fileAirPort << it->nazwa <<endl;
		fileAirPort << it->x_o <<endl;
		fileAirPort << it->x_k <<endl;
		fileAirPort << it->y_o <<endl;
		fileAirPort << it->y_k <<endl;
		fileAirPort << it->pojemnosc <<endl;
		for(int i = 0; i<10; ++i)
			fileAirPort<<it->pasy_startowe[i]<<" ";
		fileAirPort<<endl;
	}	
	fileAirPort.close();
}*/
/*****************************************************************************/
/*
void DataBase::LoadPorts(void){
	ifstream fileAirPort("airports.txt");
	lotnisko_t newAirPort;
	while(!fileAirPort.eof()){ 
		fileAirPort>>newAirPort.lotnisko_id;
		fileAirPort>>newAirPort.stan;
		fileAirPort>>newAirPort.nazwa;
		fileAirPort>>newAirPort.x_o;
		fileAirPort>>newAirPort.x_k;
		fileAirPort>>newAirPort.y_o;
		fileAirPort>>newAirPort.y_k;
		fileAirPort>>newAirPort.pojemnosc;
		for(int i = 0; i < 10; ++i)
			fileAirPort>>newAirPort.pasy_startowe[i];
		airPortList.push_back(newAirPort);
	
	}
	
	fileAirPort.close();
}*/
/*****************************************************************************/
